package sicnu.cs.ich.activity.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sicnu.cs.ich.activity.mapper.ActivityNumMapper;
import sicnu.cs.ich.activity.mapper.ActivityParticipateMapper;
import sicnu.cs.ich.activity.service.IActivityParticipateService;
import sicnu.cs.ich.api.domain.entity.ActivityParticipate;
import sicnu.cs.ich.api.domain.entity.UserInfo;
import sicnu.cs.ich.api.domain.form.PageParam;
import sicnu.cs.ich.api.domain.vo.UserInfoVo;
import sicnu.cs.ich.security.service.IUserService;

import java.util.List;

/**
 * @author 黎琦
 * @date 2021/12/20 20:57
 **/
@Service
@RequiredArgsConstructor
public class ActivityParticipateServiceImpl implements IActivityParticipateService {
    private final ActivityParticipateMapper activityParticipateMapper;
    private final ActivityNumMapper  activityNumMapper;
    private final IUserService userService;
    @Override
    public PageInfo<UserInfoVo> getParticipateList(Integer aid, PageParam param) {
        PageHelper.startPage(param.getPageNum(),param.getPageSize());
        List<UserInfoVo> uidList = activityParticipateMapper.getUserListByAid(aid);
        //for (Integer uid : uidList) {
        //    userInfoVOList.add(userService.);
        //}
        return new PageInfo<>(uidList);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void pass(List<ActivityParticipate> activityParticipates) {
        //更新活动参与者状态
        activityParticipateMapper.updateStatus(activityParticipates);
        //记录成功参与者人数
        long num = activityParticipates.stream().filter(activityParticipate -> activityParticipate.getStatus() == 1).count();
        activityNumMapper.updateNumByAid(activityParticipates.get(0).getActivityId(), (int)num);
    }
}
